package com.youmans.edumember.controller;


import com.youmans.commonutils.JwtUtils;
import com.youmans.commonutils.R;
import com.youmans.edumember.entity.UcenterMember;
import com.youmans.edumember.entity.vo.LoginVo;
import com.youmans.edumember.entity.vo.RegisterVo;
import com.youmans.edumember.service.UcenterMemberService;
import com.youmans.servicebase.exceptionHandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Youmans
 * @since 2022-05-17
 */
@Api(tags = "会员表 前端控制器")
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;

    @ApiOperation("登录")
    @PostMapping("login")
    public R loginUser(@RequestBody LoginVo member){
//        String token = memberService.login(member) ;
//        return R.ok().data("token",token) ;
        // 获取生成token值
        String token;
        try {
            token = memberService.login(member) ;
        } catch (GuliException e) {
            return R.error().message(e.getMsg());
        }
        return R.ok().data("token",token) ;
    }

    @ApiOperation("注册")
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        try{
            memberService.register(registerVo) ;

            return R.ok() ;
        }catch (GuliException e){
            e.printStackTrace();
            System.err.println(e.getMsg());
            return R.error().message(e.getMsg()) ;
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("根据token获取用户信息")
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
//        调用jwt工具类，根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member) ;
    }

    @ApiOperation("根据id获取用户信息")
    @GetMapping("getInfoUc/{id}")
    public R  getInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        UcenterMember ucenterMember = memberService.getById(id);
        return R.ok().data("items",ucenterMember);
    }

    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMember  getUserInfoOrder(@PathVariable String id) {
        //根据用户id获取用户信息
        UcenterMember ucenterMember = memberService.getById(id);
        return ucenterMember;
    }

    @ApiOperation("查询某一天注册人数")
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
       Integer count = memberService.countRegisterDay(day) ;
       return R.ok().data("countRegister",count) ;
    }
}

